package com.example.hotel_lxy.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.example.hotel_lxy.R;
import com.example.hotel_lxy.application.ARouterPath;
import com.example.hotel_lxy.bean.BeanBean;
import com.example.hotel_lxy.bean.PotentiaInfoBean;
import com.example.hotel_lxy.bean.ScheduledBean;
import com.example.hotel_lxy.net.Api;
import com.example.hotel_lxy.utils.JurisdictionUtils;
import com.example.hotel_lxy.utils.SPUtils;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.ysxsoft.common_base.adapter.BaseQuickAdapter;
import com.ysxsoft.common_base.adapter.BaseViewHolder;
import com.ysxsoft.common_base.base.BaseActivity;
import com.ysxsoft.common_base.base.frame.list.IListAdapter;
import com.ysxsoft.common_base.base.frame.list.ListManager;
import com.ysxsoft.common_base.utils.JsonUtils;
import com.ysxsoft.common_base.utils.ToastUtils;
import com.ysxsoft.common_base.view.widgets.MultipleStatusView;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.Call;

/**
 * 潜客信息
 */
@Route(path = "/main/PotentialInformationActivity")
public class PotentialInformationActivity extends BaseActivity implements IListAdapter<PotentiaInfoBean.DataBean.ListBean> {
    @BindView(R.id.back)
    ImageView back;
    ListManager<PotentiaInfoBean.DataBean.ListBean> manager;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.multipleStatusView)
    MultipleStatusView multipleStatusView;
    @BindView(R.id.text_yuding)
    TextView textYuding;
    @BindView(R.id.time_office)
    TextView timeOffice;
    @Autowired
    String idS;
    @Autowired
    String year;
    @Autowired
    String month;
    @Autowired
    String day;
    @Autowired
    String begin_night;
    @Autowired
    String b_name;
    @BindView(R.id.smartRefresh)
    SmartRefreshLayout smartRefresh;
    private String b_nameid;

    public static void start(String idS, String year, String month, String day, String begin_night, String b_name) {
        ARouter.getInstance().build(ARouterPath.getPotentialInformationActivity())
                .withString("idS", idS)
                .withString("year", year)
                .withString("month", month)
                .withString("day", day)
                .withString("begin_night", begin_night)
                .withString("b_name", b_name)
                .navigation();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_potential_information;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @Override
    public void doWork() {
        ARouter.getInstance().inject(this);
        super.doWork();
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        textYuding.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                boolean addscheduled = JurisdictionUtils.getAddscheduled(PotentialInformationActivity.this);
                if (!addscheduled) {
                    ToastUtils.longToast(PotentialInformationActivity.this, "当前账号没有操作权限哦");
                    return;
                }
                ReserveActivity.start(idS,year+"-"+month+"-"+day+"-"+begin_night);
            }
        });

        initView();
    }

    private void initListener() {
        showLoadingDialog();
        OkHttpUtils.post()
                .url(Api.POST_INDEX_SCHEDULED)
                .addHeader("flag", SPUtils.get(PotentialInformationActivity.this, "token", "").toString())
                .tag(this)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        Log.e("TAG", "宴会厅onError   " + e.getMessage());
                        hideLoadingDialog();
                        ToastUtils.shortToast(mContext,""+e.getMessage());
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        Log.e("TAG", "宴会厅onResponse   " + response);
                        hideLoadingDialog();
                        BeanBean beanBean= JsonUtils.parseByGson(response, BeanBean.class);
                        if(beanBean.getCode().equals("4")){
                            SPUtils.put(mContext,"token","");
                            Intent intent = new Intent(mContext, LoginActivity.class).
                                    setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                            startActivity(intent);
                            return;
                        }
                        ScheduledBean scheduledBean = JsonUtils.parseByGson(response, ScheduledBean.class);
                        for (int i = 0; i < scheduledBean.getData().size(); i++) {
                            if (idS.equals(scheduledBean.getData().get(i).getId() + "")) {
                                b_nameid = scheduledBean.getData().get(i).getName();
                                if (begin_night.equals("1")) {
                                    timeOffice.setText(year + "-" + month + "-" + day + " 中午         " + b_nameid + "      ");
                                } else {
                                    timeOffice.setText(year + "-" + month + "-" + day + "晚上         " + b_nameid + "      ");
                                }
                                break;
                            }
                        }
                    }
                });

    }

    private void initView() {
        manager = new ListManager(this);
        manager.init(getWindow().findViewById(android.R.id.content));
        manager.getAdapter().setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {

            }
        });
        manager.getAdapter().setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                request(manager.nextPage());
            }
        }, recyclerView);
        request(1);
    }


    @Override
    public int getItemLayoutId() {
        return R.layout.item_potental_activity;
    }
    private PotentiaInfoBean potentiaInfoBean;
    @Override
    public void request(int page) {

        OkHttpUtils.get()
                .url(Api.POST_INDEX_LATENT)
                .addHeader("flag", SPUtils.get(PotentialInformationActivity.this, "token", "").toString())
                .addParams("id", "" + idS)
                .addParams("year", "" + year)
                .addParams("month", "" + month)
                .addParams("day", "" + day)
                .addParams("begin_night", begin_night)
                .tag(this)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        Log.e("TAG", "潜客信息Exception   " + e.getMessage());
                        ToastUtils.shortToast(PotentialInformationActivity.this,e.getMessage());
                        manager.releaseRefresh();
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        Log.e("TAG", "潜客信息onResponse   " + response);

                        manager.releaseRefresh();
                        BeanBean beanBean = JsonUtils.parseByGson(response, BeanBean.class);
                        if (beanBean.getCode().equals("0")) {
                             potentiaInfoBean = JsonUtils.parseByGson(response, PotentiaInfoBean.class);

                            manager.resetPage();
                            manager.setData(potentiaInfoBean.getData().getList());
                            initListener();

                            manager.getSmartRefresh().setEnableLoadMore(false);
                            manager.getAdapter().setEnableLoadMore(false);
                            manager.getAdapter().loadMoreEnd(true);
                        }else if(beanBean.getCode().equals("4")){
                            SPUtils.put(mContext,"token","");
                            Intent intent = new Intent(mContext, LoginActivity.class).
                                    setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                            startActivity(intent);
                            return;
                        }

                    }
                });
    }

    @Override
    protected void onResume() {
        super.onResume();
        request(1);
    }

    @Override
    public void fillView(BaseViewHolder helper, PotentiaInfoBean.DataBean.ListBean s) {
        TextView text_number = helper.itemView.findViewById(R.id.text_number);
        TextView text_name = helper.itemView.findViewById(R.id.text_name);
        TextView t_name = helper.itemView.findViewById(R.id.t_name);
        TextView addtime = helper.itemView.findViewById(R.id.addtime);
        TextView text_director = helper.itemView.findViewById(R.id.text_director);
        ImageView image_type = helper.itemView.findViewById(R.id.image_type);
        int i = helper.getLayoutPosition() + 1;
        text_number.setText(i + "");
        text_name.setText("【客户姓名】" + s.getBook());
        t_name.setText("" + s.getT_name());
        addtime.setText("【预定时间】" + s.getAddtime());
        text_director.setText("【客户经理】" + s.getDirector());
        if (s.getType() == 1) {
            image_type.setVisibility(View.VISIBLE);
        } else {
            image_type.setVisibility(View.GONE);
        }
        helper.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //是否可以查看全部  1开启  0关闭
                String string = SPUtils.get(PotentialInformationActivity.this, "sayall", "").toString();
                //是否可以查看个人  1开启  0关闭
                String string1 = SPUtils.get(PotentialInformationActivity.this, "saypersonal", "").toString();
                if(string.equals("1")){
                    ReserveInformationActivity.start(s.getId() + "",s.getIs_del());
                    if(potentiaInfoBean.getData().getList().size()==1){
                        finish();
                    }
                }else{
                    if(string1.equals("1")){
                        boolean getusername = JurisdictionUtils.getusername(PotentialInformationActivity.this, s.getDirector());
                        if(getusername){
                            ReserveInformationActivity.start(s.getId() + "",s.getIs_del());
                            if(potentiaInfoBean.getData().getList().size()==1){
                                finish();
                            }
                        }else{
                            ToastUtils.longToast(PotentialInformationActivity.this, "当前账号没有操作权限哦");
                        }
                    }else{
                        ToastUtils.longToast(PotentialInformationActivity.this, "当前账号没有操作权限哦");
                    }

                }

            }
        });
    }

    @Override
    public void fillMuteView(BaseViewHolder helper, PotentiaInfoBean.DataBean.ListBean s) {

    }

    @Override
    public void attachActivity(AppCompatActivity activity) {

    }

    @Override
    public void dettachActivity() {

    }

    @Override
    public RecyclerView.LayoutManager getLayoutManager() {
        return new LinearLayoutManager(this);
    }

    @Override
    public boolean isMuteAdapter() {
        return false;
    }

    @Override
    public int[] getMuteTypes() {
        return new int[0];
    }

    @Override
    public int[] getMuteLayouts() {
        return new int[0];
    }
}